using Microsoft.Practices.Unity;
using Unity.Extensions;
using Xunit;

namespace Unity.Extensions
{
    public class InterceptInterfaceGenericMethodsInterceptionExtensionTest
    {
        [Fact]
        public void GenericMethodOnNonGenericInterface()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.RegisterType<IFoo, Foo>();

            IFoo obj = container.Resolve<IFoo>();
            obj.Bar(42);
            obj.Bar("world");

            Assert.Equal(6, Recorder.Records.Count);
            Assert.Equal("Before Method", Recorder.Records[0]);
            Assert.Equal("Passed: 42", Recorder.Records[1]);
            Assert.Equal("After Method", Recorder.Records[2]);
            Assert.Equal("Before Method", Recorder.Records[3]);
            Assert.Equal("Passed: world", Recorder.Records[4]);
            Assert.Equal("After Method", Recorder.Records[5]);
        }

        public class Foo : IFoo
        {
            [InterfaceIntercept(typeof(RecordingHandler))]
            public void Bar<T>(T value)
            {
                Recorder.Records.Add("Passed: " + value);
            }
        }

        public interface IFoo
        {
            void Bar<T>(T value);
        }
    }
}